package com.cy.milktea.pojo;


import com.cy.milktea.pojo.System.BaseObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;


@EqualsAndHashCode(callSuper = true)//将其父类属性也进行比较
@ToString(callSuper = true)//显示其父类属性
@Data
public class Menu extends BaseObject implements Serializable {

    private Integer id;  //----------->菜单编号
    private Integer parentId;  //----->父菜单编号
    private String name;  //---------->菜单名称
    private Integer sort;  //--------->排序
    private String url;  //----------->菜单路径
    private Integer type;  //--------->类型
    private Integer isShow;  //------->可见性
    private String permission;  //---->权限标识

    private List<Menu> children;  //-->子菜单列表


    /**
     * 菜单树化
     *
     * @param menus 不包含children的Menu集合,一级菜单id为0
     * @return 菜单树
     */
    public static List<Menu> toTree(List<Menu> menus) {
        menus.sort((m1, m2) -> m1.getId() < m2.getId() ? -1 : 0);
        return toTree(0, 0, menus);
    }

    private static List<Menu> toTree(int index, int pid, List<Menu> menus) {
        List<Menu> branch = new ArrayList<>();
        for (int i = index; i < menus.size(); i++) {
            Menu menu = menus.get(i);
            if (menu.getParentId() == pid) {
                menu.setChildren((toTree(i + 1, menu.getId(), menus)));
                branch.add(menu);
            }
        }
        return branch;
    }


    public static List<Map<String, Object>> toTreeLastNoChildren(List<Menu> menus) {
        return toJson(toMap(menus));
    }


    /* 配合前端elementUI的递归操作 */
    private static List<Map<String, Object>> toMap(List<Menu> menus) {
        List<Map<String, Object>> list = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Menu menu : menus) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", menu.getId());
            map.put("parentId", menu.getParentId());
            map.put("name", menu.getName());
            map.put("sort", menu.getSort());
            map.put("url", menu.getUrl());
            map.put("type", menu.getType());
            map.put("isShow", menu.getIsShow());
            map.put("permission", menu.getPermission());
            map.put("createdUser", menu.getCreatedUser());
            Date cTime = menu.getCreatedTime();
            map.put("createdTime", cTime == null ? null : format.format(cTime));
            map.put("modifiedUser", menu.getModifiedUser());
            Date mTime = menu.getModifiedTime();
            map.put("modifiedTime", mTime == null ? null : format.format(mTime));
            list.add(map);
        }
        return list;
    }

    private static List<Map<String, Object>> toJson(List<Map<String, Object>> list) {
        list.sort((m1, m2) -> (int) m1.get("id") < (int) m2.get("id") ? -1 : 0);
        return toJson(0, 0, list);
    }

    private static List<Map<String, Object>> toJson(int index, int pid, List<Map<String, Object>> list) {
        List<Map<String, Object>> branch = new ArrayList<>();
        for (int i = index; i < list.size(); i++) {
            Map map = list.get(i);
            if ((int) map.get("parentId") == pid) {
                List clist = toJson(i + 1, (int) map.get("id"), list);
                if (clist.size() != 0) {
                    map.put("children", clist);
                }
                branch.add(map);
            }
        }
        return branch;
    }

    public static List<Integer> getLastChildMenuIds(List<Menu> menus) {
        List<Integer> list = new ArrayList<>();
        menus.sort((m1, m2) -> m1.getId() < m2.getId() ? -1 : 0);
        return getChildMenuIds(0, 0, menus, list);
    }

    private static List<Integer> getChildMenuIds(Integer index, Integer pid, List<Menu> menus, List<Integer> list) {
        boolean has = false;
        for (int i = 0; i < menus.size(); i++) {
            Menu menu = menus.get(i);
            if (menu.getParentId() == pid) {
                has = true;
                getChildMenuIds(i + 1, menu.getId(), menus, list);
            }
        }
        if (!has) {
            list.add(pid);
        }
        return list;
    }



}






























